package jpb.module.rest.apply;

import jpb.constant.Reply;
import jpb.constant.ReturnState;
import jpb.constant.StatusCode;
import jpb.module.daomain.apply.Apply;
import jpb.module.daomain.organize.Organize;
import jpb.module.service.apply.ApplyService;
import jpb.security.HandleCookie;
import jpb.security.HandleToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by haol on 2017/5/7.
 */
@RestController
@RequestMapping("/api")
public class ApplyController {
        @Autowired
        private ApplyService applyService;


        HandleToken handleToken = new HandleToken();


        /**
         * @api {POST} /api/app/apply/add 成员发送申请
         * @apiGroup ApplyAPP
         * @apiName add
         * @apiDescription 成员发送申请，申请加入oid的组织
         * @apiParam {String} excuse 理由
         * @apiParam {String} oid 申请加入的组织id(not null)
         * @apiParamExample {json} 请求样例：
         * POST /api/app/apply/add
         * {
         * “oid":"organize_id",
         * "excuse":"woxihuan";
         * }
         * @apiSuccessExample {json} 返回样例:
         * {
         * "code":0,"msg":"增加成功"
         * }
         */
        @RequestMapping(value = "/app/apply/add", method = RequestMethod.POST)
        public String add(HttpServletRequest request, Apply apply,String oid) {
                String token = HandleCookie.getValueByName(request, StatusCode.COOKIE_JWT_APP);
                String num = handleToken.getValueByKey(token, StatusCode.SUBJECT_KEY);
                apply.setMemberNum(num);
                apply.setOid(oid);
                apply.setState(0);

                List<Apply> applies = applyService.getByOidAndState(apply.getOid(),0,apply.getMemberNum());
                if (applies.size() != 0){
                       for (Apply apply1:applies){
                               applyService.delete(apply1.getId());
                       }
                }


                if(!applyService.getMemByOidAndMum(oid,apply.getMemberNum())){
                        return new Reply(1, "您已存在这个组织").toJson();
                }


                Organize organize = applyService.getOrganize(oid);
                if (organize.getType() == 2){
                        return new Reply(1, "该组织可直接关注").toJson();
                }
                if (organize.getType() != 1){
                        return new Reply(1, "该组织不允许分享").toJson();
                }

                applyService.addApply(apply);
                return new Reply(0, ReturnState.INSERT_SUCCESS).toJson();
        }
        /**
         * @api {GET} /api/app/apply/i/{oid} 获取申请列表
         * @apiGroup ApplyWEB
         * @apiName getByOid
         * @apiDescription  获取id为{oid}的组织地申请列表
         * @apiParamExample {json} 请求样例：
         * GET /api/web/apply/i/organize_id
         * @apiSuccessExample {json} 返回样例:
         * {
         *              "code":0,"msg":"成功","data":[{"id":1,"mid":1,"oid":"e7e9605a-d8c1-42d8-9743-4918b84a4a5f","excuse":"I like","state":0},
         *              {"id":2,"mid":1,"oid":"e7e9605a-d8c1-42d8-9743-4918b84a4a5f","excuse":"I like i","state":0}]
         * }
         */
        @RequestMapping(value = "/web/apply/i/{oid}", method = RequestMethod.GET)
        public String getByOid(@PathVariable String oid) {

                List<Apply> applies = applyService.getApplyByOid(oid);
                Reply<List<Apply>> reply = new Reply<List<Apply>>(0,ReturnState.SUCCESS,applies);

                return reply.toJson();
        }
        /**
         * @api {GET} /api/web/apply/i/{oid}/noSee 获取未处理申请数量
         * @apiGroup ApplyWEB
         * @apiName getNoSeeCount
         * @apiDescription 获取组织中未处理的申请数量
         * @apiParamExample {json} 请求样例：
         * GET /api/web/apply/i/{oid}/noSee
         * @apiSuccessExample {json} 返回样例:
         * {"code":0,"msg":"成功","data":2}
         */
        @RequestMapping(value = "/web/apply/i/{oid}/noSee", method = RequestMethod.GET)
        public String getNoSeeCount(@PathVariable String oid) {

                List<Apply> applies = applyService.getApplyByOidAndStateIsZero(oid);
                Reply<Integer> reply = new Reply<Integer>(0,ReturnState.SUCCESS,applies.size());

                return reply.toJson();
        }
        /**
         * @api {GET} /api/web/apply/{id} 获取申请信息
         * @apiGroup Apply
         * @apiName get
         * @apiDescription 获取id为{id}的申请信息
         * @apiParamExample {json} 请求样例：
         * GET /api/app/apply/{id}
         * @apiSuccessExample {json} 返回样例:
         * {
         * "code":0,"msg":""
         * }
         */
        @RequestMapping(value = "/web/i/{oid}/{id}", method = RequestMethod.GET)
        public String get(@PathVariable Integer id) {
                Apply apply = applyService.getByOid(id);

                Reply<Apply> reply = new Reply<Apply>(0,ReturnState.SUCCESS,apply);

                return reply.toJson();
        }

        /**
         * @api {GET} /api/app/apply/see 获取其申请列表
         * @apiGroup ApplyAPP
         * @apiName getByMid
         * @apiDescription 登录用户查看自己的申请列表
         * @apiParamExample {json} 请求样例：
         * GET /api/app/apply/see
         * @apiSuccessExample {json} 返回样例:
         * {
         * "code":0,"msg":"成功"
         * }
         */
        @RequestMapping(value = "/app/apply/see", method = RequestMethod.GET)
        public String getByMid(HttpServletRequest request) {
                String token = HandleCookie.getValueByName(request, StatusCode.COOKIE_JWT_APP);
                String num = handleToken.getValueByKey(token, StatusCode.SUBJECT_KEY);
                List<Apply> applies = applyService.getByNum(num);
                Reply<List<Apply>> reply = new Reply<List<Apply>>(0,ReturnState.SUCCESS,applies);

                return reply.toJson();
        }
        /**
         * @api {PUT} /api/web/apply/update 管理员处理申请
         * @apiGroup ApplyWEB
         * @apiName update
         * @apiDescription  管理员处理申请
         * @apiParam {String} reply 回复
         * @apiParam {String} state 申请状态
         * @apiParamExample {json} 请求样例：
         * {PUT} /api/web/apply/update
         * {
         * “state":"1",
         * "reply":"woxihuan";
         * }
         * @apiSuccessExample {json} 返回样例:
         * {
         * "code":0,"msg":"更新成功"
         * }
         */
        @RequestMapping(value = "/web/apply/update", method = RequestMethod.PUT)
        public String update(HttpServletRequest request, Apply apply) {
                String token = HandleCookie.getValueByName(request, StatusCode.COOKIE_JWT_WEB);
                String sid = handleToken.getValueByKey(token, "id");
                apply.setHandleId(sid);

                applyService.update(apply);
                return new Reply(0, ReturnState.INSERT_SUCCESS).toJson();
        }

}
